package storm;

import kafka.javaapi.consumer.ConsumerConnector;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author Hanhan_Jun
 * @date 2021年12月11日 15:04
 */
public class Spout1 extends BaseRichSpout {
    //数据发送对象
    private SpoutOutputCollector collector = null;
    private ConsumerConnector consumer;

    @Override
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
        this.collector = spoutOutputCollector;
    }

    @Override
    public void nextTuple() {
        //config配置
        Properties prop=new Properties();
        //kafka集群IP地址
        prop.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.30.141:9092,192.168.30.142:9092,192.168.30.143:9092");
        //对key、value序列化
        prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        prop.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
        //超时设定
        prop.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,"30000");
        prop.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        prop.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
        //latest收最新的数据 none会报错 earliest最早的数据
        prop.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
        //分组ID
        prop.put(ConsumerConfig.GROUP_ID_CONFIG,"G11");
        //创建消费者
        KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<byte[], byte[]>(prop);
        consumer.subscribe(Collections.singleton("order"));//订阅

        while (true){
            ConsumerRecords<byte[], byte[]> poll = consumer.poll(100);
            //迭代器
            Iterator<ConsumerRecord<byte[], byte[]>> iterator = poll.iterator();
            while (iterator.hasNext()) {
                ConsumerRecord<byte[], byte[]> next = iterator.next();
                String tuple = String.valueOf(next.value());
                try {
                    //声明一个JSONObejct对象
                    JSONObject jsonObject=new net.sf.json.JSONObject();
                    //声明一个JSONArray对象
                    JSONArray jsonArray =new JSONArray();
                    //利用jsonObject方法将字符串转JSON
                    JSONObject json_line = jsonObject.fromObject(tuple);
                    //得到数据data，去除元数据
                    JSONArray data =jsonArray.fromObject(json_line.getString("data"));
                    //得到数据data各字段的值
                    //System.out.println(data.toArray()[0]);
                    String order_id =jsonObject.fromObject(data.toArray()[0]).getString("order_id");
                    String book_id=jsonObject.fromObject(data.toArray()[0]).getString("book_id");
                    String book_name =jsonObject.fromObject(data.toArray()[0]).getString("book_name");
                    String book_type =jsonObject.fromObject(data.toArray()[0]).getString("book_type");
                    int book_num=jsonObject.fromObject(data.toArray()[0]).getInt("book_num");
                    double book_price=jsonObject.fromObject(data.toArray()[0]).getDouble("book_price");
                    String book_press=jsonObject.fromObject(data.toArray()[0]).getString("book_press");
                    int id=jsonObject.fromObject(data.toArray()[0]).getInt("id");
                    //发送数据到Bolt
                    collector.emit(new Values(order_id,book_id,book_name,book_type,book_num,book_price,book_press,id));

                }catch (Exception e){
                    System.out.println("ERROR");
                }

            }
        }

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        //给下游的字段声明，和emit的value对应
        outputFieldsDeclarer.declare(new Fields
                (new String[]{"order_id","book_id","book_name","book_type","book_num", "book_price", "book_press","id"}));
    }

}
